<template>
  <div class="foodList">
    <div class="foodItem" v-for="(item, index) in foodList" :key="index">
      <div class="imgBox">
        <img :src="item.photo" alt="" />
      </div>

      <div class="name">{{ item.menuname }}</div>
      <div style="text-align: center">
        <div style="color: red">￥{{ item.price }}</div>
        <div style="font-size: 16px; padding-bottom: 10px">
          <!-- <el-input-number
            size="mini"
            v-model="item.num"
            :min="1"
            :max="10"
          ></el-input-number> -->
        </div>
        <el-button type="primary" size="mini" @click="handleAddCar(item)"
          >加入购物车</el-button
        >
        <el-button
          type="danger"
          size="mini"
          @click="handleClickMenu(item,'foodDetail')"
          >查看详情</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import bus from "../../plugins/bus";

export default {
  data() {
    return {
      num: 1,
      foodList: [
        // {
        //   name: "香辣爆炒牛蛙",
        //   price: 59,
        //   num: 1,
        //   img: require("../../assets/img/爆炒牛蛙.jpg"),
        // },
      ],
      foodClass: this.$route.query.id,
      order: [],
    };
  },
  watch: {
    $route(val) {
      if (val.path == "/foodList") {
        this.foodClass = val.query.id;
        this.getFoodList(this.foodClass);
      }
    },
  },
  created() {
    this.getFoodList(this.foodClass);
  },
  methods: {
    handleAddCar(item) {
      let obj = {
        id:item.id,
        menuname: item.menuname,
        num: item.num,
        price: item.price,
      };
      bus.$emit("add", obj);
    },
    async getFoodList(id) {
      let res = await this.$http.get(
        `/banservice/secondmenu/getsecondbyfirstid/${id}`
      );
      this.foodList = res.data.data.list;
      this.foodList.forEach((item) => {
        item.num = 1;
      });
    },
    handleClickMenu(item,url) {
      console.log(item);
      if (this.$route.path == "/" + url) return;
     // this.$router.push("/" + url);
      this.$router.push({name: url, query: {
          item: item,
        }});
    },
  },
};
</script>

<style lang="less" scoped>
.foodList {
  .foodItem {
    width: 18%;
    cursor: pointer;
    border: 1px solid #ccc;
    border-radius: 10px;
    box-sizing: border-box;
    display: inline-block;
    padding-bottom: 10px;
    overflow: hidden;
    margin: 10px;
    .imgBox {
      width: 100%;
      height: 120px;
      position: relative;
      overflow: hidden;
      &:hover {
        img {
          transform: scale(1.1);
        }
      }
      img {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        transition: all 200ms;

        object-fit: cover;
      }
    }
    .name {
      font-size: 20px;
      font-weight: 700;
      color: #212121;
      text-align: center;
      padding: 10px 0;
    }
  }
}
</style>
